#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author：albert time:2019/9/14
import json
import requests


def answer(content,city):
    api_url = "http://openapi.tuling123.com/openapi/api/v2"
    # text_input = input('我：')
    req = {
        "perception":
        {
            "inputText":
            {
                "text": content
            },

            "selfInfo":
            {
                "location":
                {
                    "city": city,
                    "province": "",
                    "street": ""
                }
            }
        },

        "userInfo":
        {
            "apiKey": "9851454c1785465783b76085f93a2630",
            "userId": "OnlyUseAlphabet"
        }
    }
    # 将字典格式的req编码为utf8
    # json.dumps()===>将python代码转换成json字符串
    req = json.dumps(req).encode('utf8')
    # print(req)

    response = requests.post(api_url, data=req, headers={'content-type': 'application/json'})
    response_str = response.content.decode('utf8')
    # 将返回的信息转换成json
    response_dic = json.loads(response_str)


    intent_code = response_dic['intent']['code']
    results_text = response_dic['results'][0]['values']['text']
    print('小白的回答：')
    # print('code：' + str(intent_code))
    print('text：' + results_text)
    return  results_text